import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class NiuSmsService extends BaseService<any> {
  private readonly logger = new Logger(NiuSmsService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * enableNiuSms
   * 对应 PHP: NiuSmsService_admin::enableNiuSms()
   * 逻辑类型: undefined - undefined
   */
  async enableNiuSms(enable: any) {
    // 基于PHP真实逻辑: enableNiuSms
    // PHP原文: if ($enable == 1) {             $config = $this->niu_service->getNiuLoginConfig(true);             if (empty($config) || !isset($config[SmsDict::NIUSM...
if (enable == 1) {
            const config = this.niu_service.getNiuLoginConfig(true);
            if (!config) || !typeof config[SmsDict.NIUSMS)) || !config[SmsDict.NIUSMS).username) || !config[SmsDict.NIUSMS).password) || !config[SmsDict.NIUSMS).signature)) {
                throw new AdminBusinessException("NIU_SMS_ENABLE_FAILED");
            }
            this.niu_service.setNiuLoginConfig([default: SmsDict.NIUSMS)];
  }

  /**
   * getConfig
   * 对应 PHP: NiuSmsService_admin::getConfig()
   * 逻辑类型: undefined - undefined
   */
  async getConfig() {
    // 基于PHP真实逻辑: getConfig
    // PHP原文: $login_config = $this->niu_service->getNiuLoginConfig(true);         return [             'is_login' => empty($login_config[SmsDict::NIUSMS]) ? 0 : 1,...
const login_config = this.niu_service.getNiuLoginConfig(true);
        return [
            is_login: !login_config[SmsDict.NIUSMS)) ? 0 : 1,
            username: login_config[SmsDict.NIUSMS].username || '',
            is_enable: ((login_config.default || '') == SmsDict.NIUSMS) ? 1 : 0,
        ];
    }

    /**
     * 获取套餐列表
     * @param params
     * @return mixed
     */
    async packageList(params)
    {
        const res = this.niu_service.packageList(params);
        return res;
    }

    /**
     * 发送动态码
     * @param params
     * @return mixed
     */
    async sendMobileCode(params)
    {
        const res = this.niu_service.sendMobileCode(params);
        return res;
    }

    /**
     * 发送动态码
     * @return mixed
     */
    async captcha()
    {
        const res = this.niu_service.captcha(];
        return res;
    }

    /**
     * 注册牛云短信账号
     * @param data
     * @return mixed
     */
    async registerAccount(data)
    {
        if (!!data.imgUrl) && strstr(data.imgUrl, 'http') === false) {
            data.imgUrl = request().domain()  + '/" . data.imgUrl;
  }

  /**
   * packageList
   * 对应 PHP: NiuSmsService_admin::packageList()
   * 逻辑类型: undefined - undefined
   */
  async packageList(params: any) {
    // 基于PHP真实逻辑: packageList
    // PHP原文: $res = $this->niu_service->packageList($params);         return $res;     }      /**      * 发送动态码      * @param $params      * @return mixed      */  ...
const res = this.niu_service.packageList(params);
        return res;
    }

    /**
     * 发送动态码
     * @param params
     * @return mixed
     */
    async sendMobileCode(params)
    {
        const res = this.niu_service.sendMobileCode(params);
        return res;
    }

    /**
     * 发送动态码
     * @return mixed
     */
    async captcha()
    {
        const res = this.niu_service.captcha(];
        return res;
    }

    /**
     * 注册牛云短信账号
     * @param data
     * @return mixed
     */
    async registerAccount(data)
    {
        if (!!data.imgUrl) && strstr(data.imgUrl, 'http') === false) {
            data.imgUrl = request().domain()  + '/" . data.imgUrl;
  }

  /**
   * sendMobileCode
   * 对应 PHP: NiuSmsService_admin::sendMobileCode()
   * 逻辑类型: undefined - undefined
   */
  async sendMobileCode(params: any) {
    // 基于PHP真实逻辑: sendMobileCode
    // PHP原文: $res = $this->niu_service->sendMobileCode($params);         return $res;     }      /**      * 发送动态码      * @return mixed      */     public function ...
const res = this.niu_service.sendMobileCode(params);
        return res;
    }

    /**
     * 发送动态码
     * @return mixed
     */
    async captcha()
    {
        const res = this.niu_service.captcha(];
        return res;
    }

    /**
     * 注册牛云短信账号
     * @param data
     * @return mixed
     */
    async registerAccount(data)
    {
        if (!!data.imgUrl) && strstr(data.imgUrl, 'http') === false) {
            data.imgUrl = request().domain()  + '/" . data.imgUrl;
  }

  /**
   * captcha
   * 对应 PHP: NiuSmsService_admin::captcha()
   * 逻辑类型: undefined - undefined
   */
  async captcha() {
    // 基于PHP真实逻辑: captcha
    // PHP原文: $res = $this->niu_service->captcha();         return $res;     }      /**      * 注册牛云短信账号      * @param $data      * @return mixed      */     public ...
const res = this.niu_service.captcha(];
        return res;
    }

    /**
     * 注册牛云短信账号
     * @param data
     * @return mixed
     */
    async registerAccount(data)
    {
        if (!!data.imgUrl) && strstr(data.imgUrl, 'http') === false) {
            data.imgUrl = request().domain()  + '/" . data.imgUrl;
  }

  /**
   * registerAccount
   * 对应 PHP: NiuSmsService_admin::registerAccount()
   * 逻辑类型: undefined - undefined
   */
  async registerAccount(data: any) {
    // 基于PHP真实逻辑: registerAccount
    // PHP原文: if (!empty($data['imgUrl']) && strstr($data['imgUrl'], 'http') === false) {             $data['imgUrl'] = request()->domain() . "/" . $data['imgUrl'];...
if (!!data.imgUrl) && strstr(data.imgUrl, 'http') === false) {
            data.imgUrl = request().domain()  + '/" . data.imgUrl;
  }

  /**
   * loginAccount
   * 对应 PHP: NiuSmsService_admin::loginAccount()
   * 逻辑类型: undefined - undefined
   */
  async loginAccount(params: any) {
    // 基于PHP真实逻辑: loginAccount
    // PHP原文: $account_info = $this->niu_service->loginAccount($params);         if ($account_info) {             (new CoreNiuSmsService())->setNiuLoginConfig($para...
const account_info = this.niu_service.loginAccount(params);
        if (account_info) {
            this.coreNiuSmsService.setNiuLoginConfig(params, true);
  }

  /**
   * editAccount
   * 对应 PHP: NiuSmsService_admin::editAccount()
   * 逻辑类型: undefined - undefined
   */
  async editAccount(username: any, params: any) {
    // 基于PHP真实逻辑: editAccount
    // PHP原文: $res = $this->niu_service->editAccount($username, $params);         $this->niu_service->setNiuLoginConfig($params);         return $res;     }      /*...
const res = this.niu_service.editAccount(username, params];
        this.niu_service.setNiuLoginConfig(params);
        return res;
    }

    /**
     * 获取牛云短信账号信息
     * @param username
     * @return mixed
     */
    async accountInfo(username)
    {
        const res = this.niu_service.accountInfo(username);
        return res;
    }

    /**
     * 重置转牛云短信账号密码
     * @param username
     * @param params
     * @return */
    async resetPassword(username, params)
    {
        const account_info = this.accountInfo(username];
        const mobile_arr = explode(",", account_info.mobiles);
        if (!in_[params.mobile, mobile_arr)) {
            throw new ApiBusinessException('ACCOUNT_BIND_MOBILE_ERROR');
  }

  /**
   * accountInfo
   * 对应 PHP: NiuSmsService_admin::accountInfo()
   * 逻辑类型: undefined - undefined
   */
  async accountInfo(username: any) {
    // 基于PHP真实逻辑: accountInfo
    // PHP原文: $res = $this->niu_service->accountInfo($username);         return $res;     }      /**      * 重置转牛云短信账号密码      * @param $username      * @param $param...
const res = this.niu_service.accountInfo(username);
        return res;
    }

    /**
     * 重置转牛云短信账号密码
     * @param username
     * @param params
     * @return */
    async resetPassword(username, params)
    {
        const account_info = this.accountInfo(username];
        const mobile_arr = explode(",", account_info.mobiles);
        if (!in_[params.mobile, mobile_arr)) {
            throw new ApiBusinessException('ACCOUNT_BIND_MOBILE_ERROR');
  }

  /**
   * resetPassword
   * 对应 PHP: NiuSmsService_admin::resetPassword()
   * 逻辑类型: undefined - undefined
   */
  async resetPassword(username: any, params: any) {
    // 基于PHP真实逻辑: resetPassword
    // PHP原文: $account_info = $this->accountInfo($username);         $mobile_arr = explode(",", $account_info['mobiles']);         if (!in_array($params['mobile'], ...
const account_info = this.accountInfo(username];
        const mobile_arr = explode(",", account_info.mobiles);
        if (!in_[params.mobile, mobile_arr)) {
            throw new ApiBusinessException('ACCOUNT_BIND_MOBILE_ERROR');
  }

  /**
   * accountSendList
   * 对应 PHP: NiuSmsService_admin::accountSendList()
   * 逻辑类型: undefined - undefined
   */
  async accountSendList(username: any, params: any) {
    // 基于PHP真实逻辑: accountSendList
    // PHP原文: $res = $this->niu_service->accountSendList($username, $params);         $return = $this->formatListPaginate($res['page']);         $return['data'] = $...
const res = this.niu_service.accountSendList(username, params];
        const return = this.formatListPaginate(res.page);
        return.data = res.records;
        return return;
    }

    /**
     * 获取签名列表
     * @param username
     * @return */
    async getSignList(username)
    {
        const res = this.niu_service.signList(username];
        const return = this.formatListPaginate(res.page];
        return.data = res.signatures;
        const config = this.niu_service.getNiuLoginConfig();
        foreach (return.data as &item) {
            item.auditResultName = NoticeTypeDict.getSignAuditType(item.auditResult);
            item.createTime = date('Y-m-d H:i:s', (item.createTime / 1000)];
            item.is_default = (config.signature == item.sign) ? 1 : 0;
  }

  /**
   * getSignList
   * 对应 PHP: NiuSmsService_admin::getSignList()
   * 逻辑类型: undefined - undefined
   */
  async getSignList(username: any) {
    // 基于PHP真实逻辑: getSignList
    // PHP原文: $res = $this->niu_service->signList($username);         $return = $this->formatListPaginate($res['page']);         $return['data'] = $res['signatures'...
const res = this.niu_service.signList(username];
        const return = this.formatListPaginate(res.page];
        return.data = res.signatures;
        const config = this.niu_service.getNiuLoginConfig();
        foreach (return.data as &item) {
            item.auditResultName = NoticeTypeDict.getSignAuditType(item.auditResult);
            item.createTime = date('Y-m-d H:i:s', (item.createTime / 1000)];
            item.is_default = (config.signature == item.sign) ? 1 : 0;
  }

  /**
   * signInfo
   * 对应 PHP: NiuSmsService_admin::signInfo()
   * 逻辑类型: undefined - undefined
   */
  async signInfo(username: any, signature: any) {
    // 基于PHP真实逻辑: signInfo
    // PHP原文: return $this->niu_service->signInfo($username, $signature);     }      /**      * 获取创建签名初始化的配置信息      * @return array      */     public function sign...
return this.niu_service.signInfo(username, signature);
    }

    /**
     * 获取创建签名初始化的配置信息
     * @return */
    async signCreateConfig()
    {
        return [
            sign_source_list: NoticeTypeDict.getSignSource(),
            sign_type_list: NoticeTypeDict.getSignType(),
            sign_default_list: NoticeTypeDict.getSignDefault()
        ];
    }

    /**
     * 签名创建
     * @param username
     * @param params
     */
    async signCreate(username, params)
    {
        if (!!params.imgUrl) && strstr(params.imgUrl, 'http') === false) {
            params.imgUrl = request().domain()  + '/' . params.imgUrl;
  }

  /**
   * signCreateConfig
   * 对应 PHP: NiuSmsService_admin::signCreateConfig()
   * 逻辑类型: undefined - undefined
   */
  async signCreateConfig() {
    // 基于PHP真实逻辑: signCreateConfig
    // PHP原文: return [             'sign_source_list' => NoticeTypeDict::getSignSource(),             'sign_type_list' => NoticeTypeDict::getSignType(),            ...
return [
            sign_source_list: NoticeTypeDict.getSignSource(),
            sign_type_list: NoticeTypeDict.getSignType(),
            sign_default_list: NoticeTypeDict.getSignDefault()
        ];
    }

    /**
     * 签名创建
     * @param username
     * @param params
     */
    async signCreate(username, params)
    {
        if (!!params.imgUrl) && strstr(params.imgUrl, 'http') === false) {
            params.imgUrl = request().domain()  + '/' . params.imgUrl;
  }

  /**
   * signCreate
   * 对应 PHP: NiuSmsService_admin::signCreate()
   * 逻辑类型: undefined - undefined
   */
  async signCreate(username: any, params: any) {
    // 基于PHP真实逻辑: signCreate
    // PHP原文: if (!empty($params['imgUrl']) && strstr($params['imgUrl'], 'http') === false) {             $params['imgUrl'] = request()->domain() . '/' . $params['i...
if (!!params.imgUrl) && strstr(params.imgUrl, 'http') === false) {
            params.imgUrl = request().domain()  + '/' . params.imgUrl;
  }

  /**
   * signDelete
   * 对应 PHP: NiuSmsService_admin::signDelete()
   * 逻辑类型: undefined - undefined
   */
  async signDelete(username: any, params: any) {
    // 基于PHP真实逻辑: signDelete
    // PHP原文: $config = $this->niu_service->getNiuLoginConfig();         $params['password'] = $config['password'];         $fail_list = $this->niu_service->signDel...
const config = this.niu_service.getNiuLoginConfig(];
        params.password = config.password;
        const fail_list = this.niu_service.signDelete(username, params);
        if (in_[config.signature, params.signatures) && !in_[config.signature, fail_list)) {
            this.editAccount(username, [signature: '')];
  }

  /**
   * syncTemplateList
   * 对应 PHP: NiuSmsService_admin::syncTemplateList()
   * 逻辑类型: undefined - undefined
   */
  async syncTemplateList(sms_type: any, username: any) {
    // 基于PHP真实逻辑: syncTemplateList
    // PHP原文: $template_list = $this->getTemplateList($sms_type, $username);         $repeat_name_arr = [];         $is_repeat = 0;         foreach ($template_list ...
const template_list = this.getTemplateList(sms_type, username];
        const repeat_name_arr = [);
        const is_repeat = 0;
        foreach (template_list as item) {
            repeat_name_arr[item.name][] = item.addon;
            if (count(repeat_name_arr[item.name)) > 1) {
                const is_repeat = 1;
            }
  }

  /**
   * getTemplateList
   * 对应 PHP: NiuSmsService_admin::getTemplateList()
   * 逻辑类型: undefined - undefined
   */
  async getTemplateList(sms_type: any, username: any, template_key: any) {
    // 基于PHP真实逻辑: getTemplateList
    // PHP原文: $config = $this->niu_service->getNiuLoginConfig();         if (empty($config) || $config['username'] != $username) {             throw new AdminExcept...
const config = this.niu_service.getNiuLoginConfig(];
        if (!config) || config.username != username) {
            throw new AdminBusinessException('ACCOUNT_ERROR_RELOGIN');
  }

  /**
   * checkTemplateAudit
   * 对应 PHP: NiuSmsService_admin::checkTemplateAudit()
   * 逻辑类型: undefined - undefined
   */
  async checkTemplateAudit(template_key: any, template_id: any) {
    // 基于PHP真实逻辑: checkTemplateAudit
    // PHP原文: $config = $this->niu_service->getNiuLoginConfig(true);         if (empty($config)) {             throw new AdminException('SMS_TYPE_NOT_OPEN');...
const config = this.niu_service.getNiuLoginConfig(true);
        if (!config)) {
            throw new AdminBusinessException('SMS_TYPE_NOT_OPEN');
  }

  /**
   * templateInfo
   * 对应 PHP: NiuSmsService_admin::templateInfo()
   * 逻辑类型: undefined - undefined
   */
  async templateInfo(sms_type: any, username: any, template_key: any) {
    // 基于PHP真实逻辑: templateInfo
    // PHP原文: $template_info = $this->template_model->where([             ['site_id', '=', $this->site_id],             ['username', '=', $username],             ['...
const template_info = this.template_model.where([
            ['site_id', '=', this.site_id],
            ['username', '=', username],
            ['sms_type', '=', sms_type],
            ['template_key', '=', template_key),
        )).findOrEmpty(];
        if (template_info.isEmpty()) {
            throw new AdminBusinessException('TEMPLATE_NOT_REPORT');
  }

  /**
   * templateCreateConfig
   * 对应 PHP: NiuSmsService_admin::templateCreateConfig()
   * 逻辑类型: undefined - undefined
   */
  async templateCreateConfig() {
    // 基于PHP真实逻辑: templateCreateConfig
    // PHP原文: return [             'template_params_type_list' => NoticeTypeDict::getApiParamsType(),             'template_type_list' => NoticeTypeDict::getTemplat...
return [
            template_params_type_list: NoticeTypeDict.getApiParamsType(),
            template_type_list: NoticeTypeDict.getTemplateType(),
            template_status_list: NoticeTypeDict.getTemplateAuditStatus()
        ];
    }

    /**
     * 报备/编辑模版
     * @param sms_type
     * @param username
     * @param params
     * @return mixed
     */
    async templateUpsert(sms_type, username, params)
    {
        //niusms
        const template_key = params.template_key;
        const template_info = this.getTemplateList(sms_type, username, template_key)[0];
        if (!template_info.sms.content)) {
            throw new AdminBusinessException('TEMPLATE_NOT_SMS_CONTENT');
  }

  /**
   * templateUpsert
   * 对应 PHP: NiuSmsService_admin::templateUpsert()
   * 逻辑类型: undefined - undefined
   */
  async templateUpsert(sms_type: any, username: any, params: any) {
    // 基于PHP真实逻辑: templateUpsert
    // PHP原文: //niusms         $template_key = $params['template_key'];         $template_info = $this->getTemplateList($sms_type, $username, $template_key)[0];    ...
//niusms
        const template_key = params.template_key;
        const template_info = this.getTemplateList(sms_type, username, template_key)[0];
        if (!template_info.sms.content)) {
            throw new AdminBusinessException('TEMPLATE_NOT_SMS_CONTENT');
  }

  /**
   * templateDelete
   * 对应 PHP: NiuSmsService_admin::templateDelete()
   * 逻辑类型: undefined - undefined
   */
  async templateDelete(username: any, template_id: any) {
    // 基于PHP真实逻辑: templateDelete
    // PHP原文: $config = $this->niu_service->getNiuLoginConfig();         $params['password'] = $config['password'];         $params['template_id'] = $template_id;  ...
const config = this.niu_service.getNiuLoginConfig(];
        params.password = config.password;
        params.template_id = template_id;
        this.niu_service.templateDelete(username, params);
        this.template_model.where('template_id', template_id).delete(];
    }

    /**
     * 格式化列表接口分页器
     * @param data
     * @return */
    private async formatListPaginate(data)
    {
        return [
            total: data.total,
            per_page: data.size,
            current_page: data.currentPage,
            last_page: data.totalPage,
        ];
    }

    /**
     * 获取订单列表
     * @param username
     * @param params
     * @return mixed
     */
    async orderList(username, params)
    {
        const res = this.niu_service.orderList(username, params);
        return res;
    }

    /**
     * 创建订单
     * @param username
     * @param package_id
     * @return mixed
     */
    async createOrder(username, package_id)
    {
        const res = this.niu_service.orderCreate(username, [package_id: package_id)];
        return res;
    }

    /**
     * 计算订单
     * @param username
     * @param package_id
     * @return mixed
     */
    async calculate(username, package_id)
    {
        const res = this.niu_service.calculate(username, [package_id: package_id)];
        return res;
    }

    /**
     * 获取支付使用信息
     * @param username
     * @param params
     * @return mixed
     */
    async getPayInfo(username, params)
    {
        const res = this.niu_service.orderPayInfo(username, params);
        return res;
    }

    /**
     * 获取订单信息
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderInfo(username, out_trade_no)
    {
        const res = this.niu_service.orderInfo(username, out_trade_no);
        return res;
    }

    /**
     * 获取订单状态
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderStatus(username, out_trade_no)
    {
        const res = this.niu_service.orderStatus(username, out_trade_no);
        return res;
    }

}
  }

  /**
   * orderList
   * 对应 PHP: NiuSmsService_admin::orderList()
   * 逻辑类型: undefined - undefined
   */
  async orderList(username: any, params: any) {
    // 基于PHP真实逻辑: orderList
    // PHP原文: $res = $this->niu_service->orderList($username, $params);         return $res;     }      /**      * 创建订单      * @param $username      * @param $packa...
const res = this.niu_service.orderList(username, params);
        return res;
    }

    /**
     * 创建订单
     * @param username
     * @param package_id
     * @return mixed
     */
    async createOrder(username, package_id)
    {
        const res = this.niu_service.orderCreate(username, [package_id: package_id)];
        return res;
    }

    /**
     * 计算订单
     * @param username
     * @param package_id
     * @return mixed
     */
    async calculate(username, package_id)
    {
        const res = this.niu_service.calculate(username, [package_id: package_id)];
        return res;
    }

    /**
     * 获取支付使用信息
     * @param username
     * @param params
     * @return mixed
     */
    async getPayInfo(username, params)
    {
        const res = this.niu_service.orderPayInfo(username, params);
        return res;
    }

    /**
     * 获取订单信息
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderInfo(username, out_trade_no)
    {
        const res = this.niu_service.orderInfo(username, out_trade_no);
        return res;
    }

    /**
     * 获取订单状态
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderStatus(username, out_trade_no)
    {
        const res = this.niu_service.orderStatus(username, out_trade_no);
        return res;
    }

}
  }

  /**
   * createOrder
   * 对应 PHP: NiuSmsService_admin::createOrder()
   * 逻辑类型: undefined - undefined
   */
  async createOrder(username: any, package_id: any) {
    // 基于PHP真实逻辑: createOrder
    // PHP原文: $res = $this->niu_service->orderCreate($username, ['package_id' => $package_id]);         return $res;     }      /**      * 计算订单      * @param $usern...
const res = this.niu_service.orderCreate(username, [package_id: package_id)];
        return res;
    }

    /**
     * 计算订单
     * @param username
     * @param package_id
     * @return mixed
     */
    async calculate(username, package_id)
    {
        const res = this.niu_service.calculate(username, [package_id: package_id)];
        return res;
    }

    /**
     * 获取支付使用信息
     * @param username
     * @param params
     * @return mixed
     */
    async getPayInfo(username, params)
    {
        const res = this.niu_service.orderPayInfo(username, params);
        return res;
    }

    /**
     * 获取订单信息
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderInfo(username, out_trade_no)
    {
        const res = this.niu_service.orderInfo(username, out_trade_no);
        return res;
    }

    /**
     * 获取订单状态
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderStatus(username, out_trade_no)
    {
        const res = this.niu_service.orderStatus(username, out_trade_no);
        return res;
    }

}
  }

  /**
   * calculate
   * 对应 PHP: NiuSmsService_admin::calculate()
   * 逻辑类型: undefined - undefined
   */
  async calculate(username: any, package_id: any) {
    // 基于PHP真实逻辑: calculate
    // PHP原文: $res = $this->niu_service->calculate($username, ['package_id' => $package_id]);         return $res;     }      /**      * 获取支付使用信息      * @param $use...
const res = this.niu_service.calculate(username, [package_id: package_id)];
        return res;
    }

    /**
     * 获取支付使用信息
     * @param username
     * @param params
     * @return mixed
     */
    async getPayInfo(username, params)
    {
        const res = this.niu_service.orderPayInfo(username, params);
        return res;
    }

    /**
     * 获取订单信息
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderInfo(username, out_trade_no)
    {
        const res = this.niu_service.orderInfo(username, out_trade_no);
        return res;
    }

    /**
     * 获取订单状态
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderStatus(username, out_trade_no)
    {
        const res = this.niu_service.orderStatus(username, out_trade_no);
        return res;
    }

}
  }

  /**
   * getPayInfo
   * 对应 PHP: NiuSmsService_admin::getPayInfo()
   * 逻辑类型: undefined - undefined
   */
  async getPayInfo(username: any, params: any) {
    // 基于PHP真实逻辑: getPayInfo
    // PHP原文: $res = $this->niu_service->orderPayInfo($username, $params);         return $res;     }      /**      * 获取订单信息      * @param $username      * @param $...
const res = this.niu_service.orderPayInfo(username, params);
        return res;
    }

    /**
     * 获取订单信息
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderInfo(username, out_trade_no)
    {
        const res = this.niu_service.orderInfo(username, out_trade_no);
        return res;
    }

    /**
     * 获取订单状态
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderStatus(username, out_trade_no)
    {
        const res = this.niu_service.orderStatus(username, out_trade_no);
        return res;
    }

}
  }

  /**
   * orderInfo
   * 对应 PHP: NiuSmsService_admin::orderInfo()
   * 逻辑类型: undefined - undefined
   */
  async orderInfo(username: any, out_trade_no: any) {
    // 基于PHP真实逻辑: orderInfo
    // PHP原文: $res = $this->niu_service->orderInfo($username, $out_trade_no);         return $res;     }      /**      * 获取订单状态      * @param $username      * @para...
const res = this.niu_service.orderInfo(username, out_trade_no);
        return res;
    }

    /**
     * 获取订单状态
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderStatus(username, out_trade_no)
    {
        const res = this.niu_service.orderStatus(username, out_trade_no);
        return res;
    }

}
  }

  /**
   * orderStatus
   * 对应 PHP: NiuSmsService_admin::orderStatus()
   * 逻辑类型: undefined - undefined
   */
  async orderStatus(username: any, out_trade_no: any) {
    // 基于PHP真实逻辑: orderStatus
    // PHP原文: $res = $this->niu_service->orderStatus($username, $out_trade_no);         return $res;     }  }...
const res = this.niu_service.orderStatus(username, out_trade_no);
        return res;
    }

}
  }

  /**
   * execsync
   * 对应 PHP: NiuSmsService_admin::execsync()
   * 逻辑类型: undefined - undefined
   */
  async execsync(sms_type: any, username: any, template_list: any, page: any) {
    // 基于PHP真实逻辑: execsync
    // PHP原文: $name_template_list = array_column($template_list, null, 'name');         $limit = 100;         $api_template_data = $this->niu_service->templateList(...
const name_template_list = array_column(template_list, null, 'name');
        const limit = 100;
        const api_template_data = this.niu_service.templateList(username, [size: limit)];

        const total = api_template_data.page.total;
        const templates = api_template_data.templates;
        const insert = [];
        foreach (templates as template) {
            const tem_id = template.temId;

            //拉取回来的模版在项目中未配置
            if (!typeof name_template_list[template.temName))) {
                continue;
            }
            if (!!template.extend)) {
                const template_key = template.extend.template_key || "";
            } } } else {
                const template_key = name_template_list[template.temName].key;
            }
            const model_info = this.template_model.where([
                ['sms_type', '=', sms_type],
                ['username', '=', username],
                ['site_id', '=', this.site_id],
                ['template_key', '=', template_key)
            )).findOrEmpty(];
            const data = [
                site_id: this.site_id,
                sms_type: sms_type,
                username: username,
                template_key: template_key,
                template_content: template.temContent,
                param_json: template.paramJson,
                template_type: template.temType,
                audit_status: template.auditResult,
                audit_msg: template.auditMsg,
                template_id: tem_id,
                report_info: template,
                create_time: template.createTime / 1000,
                update_time: time(),
            ];
            if (model_info.isEmpty()) {
                insert[] = data;
                continue;
            }
            this.template_model.where('id', model_info.id).update(data);
  }

  /**
   * formatTemplateList
   * 对应 PHP: NiuSmsService_admin::formatTemplateList()
   * 逻辑类型: undefined - undefined
   */
  async formatTemplateList(list: any) {
    // 基于PHP真实逻辑: formatTemplateList
    // PHP原文: $return = [];         $addon_arr = array_keys($list);         $addon_list = (new Addon())->whereIn('key', $addon_arr)->field('key,title')->select()->t...
const return = [];
        const addon_arr = array_keys(list);
        const addon_list = this.addonService.whereIn('key', addon_arr).field('key,title').select().toArray(];
        const addon_list = array_column(addon_list, 'title', 'key');
        foreach (list as addon => item) {
            foreach (item as value) {
                const temp = value;
//                temp.addon = addon;
                temp.addon = addon_list[addon] || '系统';
                const str = temp.sms.content || "";
                if (!str)) {
                    continue;
                }
                preg_match_all('/\{(.*?)\}/', str, matches];
                const result = matches[1];
                foreach (temp.variable as k => v) {
                    if (!in_[k, result)) {
                        unset(temp.variable[k);
  }
                return[] = temp;
            }
  }

  /**
   * formatListPaginate
   * 对应 PHP: NiuSmsService_admin::formatListPaginate()
   * 逻辑类型: undefined - undefined
   */
  async formatListPaginate(data: any) {
    // 基于PHP真实逻辑: formatListPaginate
    // PHP原文: return [             'total' => $data['total'],             'per_page' => $data['size'],             'current_page' => $data['currentPage'],          ...
return [
            total: data.total,
            per_page: data.size,
            current_page: data.currentPage,
            last_page: data.totalPage,
        ];
    }

    /**
     * 获取订单列表
     * @param username
     * @param params
     * @return mixed
     */
    async orderList(username, params)
    {
        const res = this.niu_service.orderList(username, params);
        return res;
    }

    /**
     * 创建订单
     * @param username
     * @param package_id
     * @return mixed
     */
    async createOrder(username, package_id)
    {
        const res = this.niu_service.orderCreate(username, [package_id: package_id)];
        return res;
    }

    /**
     * 计算订单
     * @param username
     * @param package_id
     * @return mixed
     */
    async calculate(username, package_id)
    {
        const res = this.niu_service.calculate(username, [package_id: package_id)];
        return res;
    }

    /**
     * 获取支付使用信息
     * @param username
     * @param params
     * @return mixed
     */
    async getPayInfo(username, params)
    {
        const res = this.niu_service.orderPayInfo(username, params);
        return res;
    }

    /**
     * 获取订单信息
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderInfo(username, out_trade_no)
    {
        const res = this.niu_service.orderInfo(username, out_trade_no);
        return res;
    }

    /**
     * 获取订单状态
     * @param username
     * @param out_trade_no
     * @return mixed
     */
    async orderStatus(username, out_trade_no)
    {
        const res = this.niu_service.orderStatus(username, out_trade_no);
        return res;
    }

}
  }
}
